<?php

namespace App\Services\Common;

use Illuminate\Http\Request;
use OSS\Core\OssException;
use OSS\OssClient;
class Upload
{
    public static $allowmimeimgType = ['image/png', 'image/jpg', 'image/jpeg'];
    public static $allowmimevideoType = ['video/mpeg', 'video/mp4'];


    public static $allowmimeFileType = ['video/mpeg', 'video/mp4'];


    public static function uploadToOss(Request $request){
        $accessKeyId=env('ALI_OSS_KEY');
        $accessKeySecret=env('ALI_OSS_SECRET');
        $endpoint=env('ALI_OSS_ENDPOINT');
        $bucket=env('ALI_OSS_BUCKET');
        $file = $request->file('file');
        if($file){
            $filePath=$file->getRealPath();
            $filename='huacunsite/'.date('Ym').'/'.md5($file->getFilename().time().mt_rand(1000,9999)).'.'.$file->getClientOriginalExtension();
            try {
                $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint.'.aliyuncs.com');
                $ossClient->uploadFile($bucket, $filename, $filePath);
            } catch (OssException $e) {
                return ['code' => 1 , 'msg' => '上传失败!!' ];
            }
            return ['code' => 0 , 'msg' => '上传成功' , 'path' =>env('ALI_OSS_URL').$filename,'filename'=>$file->getClientOriginalName()];
        }else{
            return ['code' => 500 , 'msg' => '非法操作' ];
        }

    }

    public static function uploadMedia(Request $request)
    {
        $file = $request->file('file');
        if ($file && (in_array($file->getMimeType(), static::$allowmimeimgType) || in_array($file->getMimeType(), static::$allowmimevideoType))) {
            if ((in_array($file->getMimeType(), static::$allowmimeimgType) && $file->getSize() > env('IMG_MAX_SIZE')) || (in_array($file->getMimeType(), static::$allowmimevideoType) && $file->getSize() > env('VIDEO_MAX_SIZE'))) {
                return ['code' => 1, 'msg' => '上传超出限制'];
            }
            $path = public_path();
            $keydate = '/uploadlocal/' . date('Ym') . '/';
            if (!is_dir($path . $keydate)) {
                mkdir($path . $keydate);
            }
            $filename = md5($file->getFilename() . time() . mt_rand(1000, 9999)) . '.' . $file->getClientOriginalExtension();
            move_uploaded_file($_FILES['file']['tmp_name'], $path . $keydate . $filename);
            $filename = $keydate . $filename;

            return ['code' => 0, 'msg' => '上传成功', 'path' => $filename];
        } else {
            return ['code' => 500, 'msg' => '非法操作'];
        }
    }


    public static function uploadFile(Request $request)
    {
        $file = $request->file('file');
        if ($file ) {//&& (in_array($file->getMimeType(), static::$allowmimeFileType))
            if ($file->getSize() > env('FILE_MAX_SIZE')) {
                return ['code' => 1, 'msg' => '上传超出限制'];
            }
            $filename = md5($file->getFilename() . time() . mt_rand(1000, 9999)) . '.' . $file->getClientOriginalExtension();

            $filename=$file->storeAs(date('Ym'),$filename);

            return ['code' => 0, 'msg' => '上传成功', 'path' => '/uploadlocalfile/'.$filename];
        } else {
            return ['code' => 500, 'msg' => '非法操作'];
        }
    }


    public static function uploadFileold(Request $request)
    {
        $file = $request->file('file');
        if ($file ) {//&& (in_array($file->getMimeType(), static::$allowmimeFileType))
            if ( $file->getSize() > env('FILE_MAX_SIZE')) {
                return ['code' => 1, 'msg' => '上传超出限制'];
            }
            $path = storage_path();
            $keydate = '/uploadlocalfile/' . date('Ym') . '/';
            if (!is_dir($path . $keydate)) {
                mkdir($path . $keydate);
            }
            $filename = md5($file->getFilename() . time() . mt_rand(1000, 9999)) . '.' . $file->getClientOriginalExtension();
            move_uploaded_file($_FILES['file']['tmp_name'], $path . $keydate . $filename);
            $filename = $keydate . $filename;

            return ['code' => 0, 'msg' => '上传成功', 'path' => $filename];
        } else {
            return ['code' => 500, 'msg' => '非法操作'];
        }
    }
}
